package blog.service.impl;

import blog.dao.mapper.SysUserMapper;
import blog.dao.pojo.SysUser;
import blog.service.SysUserService;
import blog.utils.JwtUtils;
import blog.vo.ErrorCode;
import blog.vo.LoginUserVo;
import blog.vo.Result;
import blog.vo.UserVo;
import blog.vo.params.LoginParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    // 通过文章id找到作者
    @Override
    public SysUser findUserByArticleId(Long id) {
        return sysUserMapper.selectById(id);
    }

    // 通过账号、密码查看是否有该用户
    @Override
    public SysUser finUser(String account,String pwd) {

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.eq(SysUser::getPassword,pwd);

        queryWrapper.select(SysUser::getId,SysUser::getAccount,SysUser::getAvatar,SysUser::getNickname);

        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);

        return sysUser;
    }

    // 通过token查找用户信息
    @Override
    public Result getUserInfoByToken(String token) throws JsonProcessingException {

        // 检验token是否为空、是否合法
        Map<String, Object> map = JwtUtils.checkToken(token);

        if (map==null) return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());

        // redis中是否还有该用户信息
        String userInfo = redisTemplate.opsForValue().get("TOKEN_" + token);

        if (userInfo==null) return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg()); // redis没有用户信息

        ObjectMapper writeMapper = new ObjectMapper();
        SysUser sysUser = writeMapper.readValue(userInfo, SysUser.class);

        LoginUserVo userVo = new LoginUserVo();
        userVo.setAccount(sysUser.getAccount());
        userVo.setNickname(sysUser.getNickname());
        userVo.setAvatar(sysUser.getAvatar());

        return Result.success(userVo); // 验证成功返回用户信息
    }

    // 通过账号查找用户信息
    @Override
    public SysUser findUserByAccount(String account) {

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getAccount,account);

        SysUser sysUser = sysUserMapper.selectOne(wrapper);

        return sysUser;
    }

    // 注册用户信息
    @Override
    public void save(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
    }

    @Override
    public UserVo findUserVoById(Long id) {

        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            sysUser = new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/img/logo.b3a48c0.png");
            sysUser.setNickname("邪眸白虎");
        }

        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);

        return userVo;
    }
}
